package com.mzwise.tunnel.service;

import cn.hutool.json.JSONObject;
import com.github.wxpay.sdk.WXPay;
import com.mzwise.tunnel.service.Configs;
import com.mzwise.tunnel.wx.v3.WxV3Client;
import com.mzwise.tunnel.wx.v3.WxV3Response;
import com.mzwise.tunnel.wx.v3.request.CreateStockRequest;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * v3 通用接口相关类
 */
public class V3CommonService {

    public V3CommonService(Configs configs) {
        if (configs.getWxV3Client() != null) {
            try {
                wxV3Client = configs.getWxV3Client();
                System.out.println("tunnel:微信v3通道开启");
            } catch (Exception e) {
                System.out.println("tunnel:微信v3通道开启错误");
                e.printStackTrace();
            }
        }
    }

    private WxV3Client wxV3Client;

    /**
     * 创建卡卷批次
     * @param request
     * @return
     * @throws IOException
     */
    public WxV3Response createStock(CreateStockRequest request) throws IOException {
        System.out.println("request:" + request);
        V3HttpPost v3HttpPost = new V3HttpPost("/marketing/favor/coupon-stocks", request.toString());
        return wxV3Client.execute(v3HttpPost.toHttp());
    }

    /**
     * 激活卡卷批次
     * @param stockId
     * @return
     * @throws IOException
     */
    public WxV3Response activeStock(String stockId) throws IOException {
        String params = "{\n" +
                "  \"stock_creator_mchid\":\"" + wxV3Client.getMerchantId() + "\"\n" +
                "}";
        V3HttpPost v3HttpPost = new V3HttpPost("/marketing/favor/stocks/" + stockId + "/start", params);
        return wxV3Client.execute(v3HttpPost.toHttp());
    }

    /**
     * 暂定卡卷批次
     * @param stockId
     * @return
     * @throws IOException
     */
    public WxV3Response pauseStock(String stockId) throws IOException {
        String params = "{\n" +
                "  \"stock_creator_mchid\":\"" + wxV3Client.getMerchantId() + "\"\n" +
                "}";
        V3HttpPost v3HttpPost = new V3HttpPost("/marketing/favor/stocks/" + stockId + "/pause", params);
        return wxV3Client.execute(v3HttpPost.toHttp());
    }

    /**
     * 重启卡卷批次
     * @param stockId
     * @return
     * @throws IOException
     */
    public WxV3Response restartStock(String stockId) throws IOException {
        String params = "{\n" +
                "  \"stock_creator_mchid\":\"" + wxV3Client.getMerchantId() + "\"\n" +
                "}";
        V3HttpPost v3HttpPost = new V3HttpPost("/marketing/favor/stocks/" + stockId + "/restart", params);
        return wxV3Client.execute(v3HttpPost.toHttp());
    }

    /**
     * 发送卡卷
     * @param openid
     * @param stockId
     * @param outRequestNo
     * @return
     * @throws IOException
     */
    public WxV3Response sendStock(String openid, String stockId, String outRequestNo) throws IOException {
        String params = "{\n" +
                "  \"stock_id\": \"" + stockId + "\",\n" +
                "  \"out_request_no\": \"" + outRequestNo + "\",\n" +
                "  \"appid\": \"" + wxV3Client.getAppid() + "\",\n" +
                "  \"stock_creator_mchid\": \"" + wxV3Client.getMerchantId() + "\"\n" +
                "}\n";
        V3HttpPost v3HttpPost = new V3HttpPost("/marketing/favor/users/" + openid + "/coupons", params);
        return wxV3Client.execute(v3HttpPost.toHttp());
    }

    /**
     * 设置商户回调地址
     * @param mchId
     * @param notifyUrl
     * @return
     * @throws IOException
     */
    public WxV3Response setCouponCallback(String mchId, String notifyUrl) throws IOException {
        String params = "{\n" +
                "  \"mchId\": \"" + mchId + "\",\n" +
                "  \"notifyUrl\": \"" + notifyUrl + "\"\n" +
                "}\n";
        V3HttpPost v3HttpPost = new V3HttpPost("/marketing/favor/callbacks", params);
        return wxV3Client.execute(v3HttpPost.toHttp());
    }


    /**
     * 进件
     * @param wxIncoming
     * @return
     * @throws Exception
     */
    public Object incoming(JSONObject wxIncoming) throws Exception {
        V3HttpPost v3HttpPost = new V3HttpPost("/applyment4sub/applyment/", wxIncoming.toString());
        return wxV3Client.execute(v3HttpPost.toHttp(), true);
    }

    /**
     * 进件状态查询
     * @param applymentId
     * @return
     * @throws Exception
     */
    public Object incomingInquire(String applymentId) throws Exception {
        V3HttpGet v3HttpGet = new V3HttpGet("/applyment4sub/applyment/applyment_id/" + applymentId);
        return wxV3Client.execute(v3HttpGet.toHttp(), true);
    }

    /**
     * 通用post请求
     */
    private class V3HttpPost {
        public V3HttpPost(String url, String params) {
            this.url = url;
            this.params = params;
        }

        private String url;
        private String params;

        public HttpPost toHttp() {
            HttpPost httpPost = new HttpPost(wxV3Client.getBaseUrl() + url);
            StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
            httpPost.setEntity(entity);
            return httpPost;
        }
    }

    /**
     * 通用get请求
     */
    private class V3HttpGet {
        public V3HttpGet(String url) {
            this.url = url;
        }
        private String url;

        public HttpGet toHttp() {
            HttpGet httpGet = new HttpGet(wxV3Client.getBaseUrl() + url);
            return httpGet;
        }
    }
}
